import { app, BrowserWindow, ipcMain, Menu, Notification } from "electron"
import path from "path"

let win;

interface notificationType {
    title: string,
    body: string,
    silent: boolean,
    urgency: 'normal' | 'critical' | 'low',
    closeButtonText: string,
    hasReply: boolean
}


let progressInterval
let width = 1200;
let height = 800;
const createWindow = () => {
    win = new BrowserWindow({
        width: width,
        height: height,
        frame: false,
        titleBarStyle: "hidden",
        webPreferences: {
            contextIsolation: true, // 是否开启隔离上下文
            nodeIntegration: true, // 渲染进程使用Node API
            preload: path.join(__dirname, "./preload.js"), // 需要引用js文件
        },
    })

    // 如果打包了，渲染index.html
    if (process.env.NODE_ENV !== 'development') {
        win.loadFile(path.join(__dirname, "./index.html"))
        win.webContents.openDevTools()
    } else {
        let url = "http://localhost:5173" // 本地启动的vue项目路径。注意：vite版本3以上使用的端口5173；版本2用的是3000
        win.loadURL(url)
        win.webContents.openDevTools()
    }




    //进度条
    const INCREMENT = 0.03
    const INTERVAL_DELAY = 100 // ms

    let c = 0
    progressInterval = setInterval(() => {
        // update progress bar to next value
        // values between 0 and 1 will show progress, >1 will show indeterminate or stick at 100%
        win.setProgressBar(c)

        // increment or reset progress bar
        if (c < 2) {
            c += INCREMENT
        } else {
            c = (-INCREMENT * 5) // reset to a bit less than 0 to show reset state
        }
    }, INTERVAL_DELAY)
}


//苹果dock栏
const dockMenu = Menu.buildFromTemplate([
    {
        label: 'New Window',
        click() { console.log('New Window') }
    }, {
        label: 'New Window with Settings',
        submenu: [
            { label: 'Basic' },
            { label: 'Pro' }
        ]
    },
    { label: 'New Command...' }
])



function showNotification(msg: string) {

    let options: notificationType = {
        title: '通知',
        body: msg,
        silent: false,
        urgency: "critical",
        closeButtonText: '关闭',
        hasReply: true
    }

    //通知
    let notification = new Notification(options)

    //判断是否支持通知
    if (Notification.isSupported()) {
        notification.show()
    } else {
        console.log("不支持通知")
    }

    //通知点击事件
    notification.on('click', () => {
        console.log('Notification clicked')
    })
    //通知关闭事件
    notification.on('close', () => {
        console.log('Notification closed')
    })

    //通知回复事件
    notification.on('reply', (event, reply: string) => {
        console.log('Notification replied', reply)
    })


}







ipcMain.on('setMinSize', (event, title) => {
    console.log("主线程监听到了vue传过来的数据", title)
    win.unmaximize()
})
ipcMain.on('setMaxSize', () => {
    console.log("主线程监听到了vue传放大的信号")
    if (win.isMaximized()) {
        win.restore();
    } else {
        win.maximize();
    }
})


ipcMain.on('setNotify', (event, msg: string) => {
    console.log("主线程已接收到", msg)
    showNotification(msg)
})


app.whenReady().then(() => {
    //增加一个dock
    if (process.platform === 'darwin') {
        app.dock.setMenu(dockMenu)
    }
    createWindow() // 创建窗口
    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})

// 关闭窗口
app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
        app.quit()
    }
}
)
//打开url
app.on('open-url', function (event, url) {
    event.preventDefault();
    console.log(`Received URL: ${url}`);
    // Handle the URL as needed
});

// before the app is terminated, clear both timers
app.on('before-quit', () => {
    clearInterval(progressInterval)
})